package mt3045;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.LinkedList;

class Pos{
	int x;
	int y;
	//Pos类下面的代码是让eclipse自动生成的，不需要自己写
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public Pos(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	
}
public class Main {
	//输入优化
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static StreamTokenizer st = new StreamTokenizer(br);
	static int rd() throws IOException {
		st.nextToken();
		return (int) st.nval;
	}
	
	
	
	
	static int n;
	static int D;
	//初始取最大值
	static int ans = Integer.MAX_VALUE;
	static Pos[] p;
	
	static Deque<Integer> maxn = new LinkedList<Integer>();
	static Deque<Integer> minn = new LinkedList<Integer>();
	public static void main(String[] args) throws IOException {
		//输入优化
//        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        
//        String[] lineArr = br.readLine().split(" ");
//        n = Integer.parseInt(lineArr[0]);
//        D = Integer.parseInt(lineArr[1]);
		n = rd();
		D = rd();
        
        //初始化Pos数组
        p = new Pos[n];
        
        //读取数据
        for(int i=0;i<n;i++) {
//        	lineArr = br.readLine().split(" ");
//        	int x = Integer.parseInt(lineArr[0]);
//            int y = Integer.parseInt(lineArr[1]);
//            p[i] = new Pos(x,y);
        	p[i] = new Pos(rd(),rd());
        }
        
        //数组排序，依据是x坐标
        Arrays.sort(p,new Comparator<Pos>(){
			@Override
			public int compare(Pos p1, Pos p2) {
				if (p1.getX() - p2.getX() >= 0) {//比较数组元素值
                    return 1;  //o1大时，返回正数表示p1往后排，升序
                } else {
                    return -1;
                }
			}
        });
        
        //开始区间最值检测
        int L= 0; //数组下标从0开始
        
        for(int i=0;i<n;i++) { //区间长度从0到n开始检测
        	//单调队列，维护区间内时间的最大最小
        	while(!maxn.isEmpty()&&p[maxn.peekLast()].y < p[i].y) {
        		maxn.pollLast();
        	}
        	maxn.addLast(i);
        	
        	while(!minn.isEmpty()&&p[minn.peekLast()].y > p[i].y) {
        		minn.pollLast();
        	}
        	minn.addLast(i);
        	
        	//出现满足条件的区间时从左开始减小区间
        	//固定区间右边的同时，逐渐缩小左边，找符合的最短区间
        	//类似双指针操作
        	while(L<i&&p[maxn.peekFirst()].y - p[minn.peekFirst()].y>=D) {
        		ans = Math.min(ans,p[i].x - p[L].x); //当前右端点和左端点间的长度“脸盆宽度”
        		L++; //左端点右移
        		//单调队列，区间外的最值出队列
        		while(!maxn.isEmpty()&&maxn.peekFirst()<L) {
        			maxn.pollFirst();
        		}
        		while(!minn.isEmpty()&&minn.peekFirst()<L) {
        			minn.pollFirst();
        		}
        	}
        	
        }
        
        if(ans == Integer.MAX_VALUE) { //ans还是初始值表示无解
        	System.out.println(-1);
        }else {
        	System.out.println(ans);
        }
        
        
	}

}
